// pages/orderConfirm/orderConfirm.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponText: "使用优惠券 >>"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.getShopCart();
    that.setData({
      totalAmount: options.money,
      money: options.money
    })
  },
  getShopCart: function(e) {
    var that = this;
    wx.request({
      url: app.globalData.ip + "/api/shopcart",
      method: "GET",
      header: {
        "X-Token": app.globalData.token
      },
      success: e => {
        // console.log(e.data.data);
        that.setData({
          shopcart: e.data.data,
        })
      }
    })
  },
  postOrder: function() { //提交订单
    var _url = app.globalData.ip + '/api/order/';
    if (this.data.currentCouponId != undefined){
      _url = _url + "?couponId=" + this.data.currentCouponId;
    }
    wx.request({
      url: _url,
      method: 'POST',
      header: {
        "X-Token": app.globalData.token
      },
      success: e => {
        wx.switchTab({
          url: '../menu/menu',
          success: function(e) {
            var page = getCurrentPages().pop();
            if (page == null || page == undefined) {
              return;
            }
            page.onShow();
            page.finishedPay();
          }
        })
      }
    })
  },
  cancel: function() {
    wx.switchTab({
      url: '../menu/menu',
    })
  },
  getMyCoupon: function() {
    var that = this;
    wx.request({
      url: app.globalData.ip + '/api/coupon',
      method: "GET",
      header: {
        "X-Token": app.globalData.token
      },
      success: e => {
        // console.log("fuck")
        var _couponStr = ["不使用优惠券"];
        var _couponArray = e.data.data;
        console.log(e)
        for (var i in _couponArray) {
          if (that.data.money < parseFloat(_couponArray[i].mix_limit)) {
            _couponStr.push("满" + _couponArray[i].mix_limit + "减" + _couponArray[i].quota + "可用(不满足满减条件）");
          } else {
            _couponStr.push("满" + _couponArray[i].mix_limit + "减" + _couponArray[i].quota + "可用");
          }
        }
        that.setData({
          couponArray: _couponArray,
          couponStr: _couponStr
        })
        that.clickCoupon(_couponStr);
      }
    })
  },
  clickCoupon: function(couponStr) {
    var index = -1;
    var that = this;
    wx.showActionSheet({
      itemList: couponStr,
      success(res) {
        index = res.tapIndex;
        if (index != 0) {
          if (that.data.couponArray[index - 1].mix_limit <= that.data.money) {
            //选择的优惠券满足条件
            // console.log(that.data.couponArray[index - 1].coupon_series_id);
            that.setData({
              money: that.data.totalAmount - that.data.couponArray[index].quota,
              couponText: that.data.couponArray[index].name + ": -" + that.data.couponArray[index].quota,
              currentCouponId: that.data.couponArray[index - 1].coupon_series_id
            })
          } else {
            that.setData({
              money: that.data.totalAmount,
              couponText: "使用优惠券 >>"
            })
          }
        } else {
          that.setData({
            money: that.data.totalAmount,
            couponText: "使用优惠券 >>"
          })
        }
      },
      fail(res) {
        index = -1;
      }
    })
    // return index;
  }
})